package foxmob.mooc;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class AppCfg {

    public  static String sDbPort, sDbUser, sDbPwd,
            sDbHost,sUploadPath,sDbName,sUploadTmpPath, sFileStorePath,
            sWxUser,sWxKey,sWxToken,sWxAesKey;

    public static void init() {

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                boolean bFileStorePath = false
                        , bDbPwd = false
                        , bDbHost = false
                        , bDbUser = false
                        , bDbPort = false
                        , bDbName =false
                        ,bWxUser = false
                        , bWxKey = false
                        , bWxToken = false
                        , bWxAesKey = false
                        , bUploadTmpPath = false;

                public void startElement(String uri, String localName,String qName,
                                         Attributes attributes) throws SAXException {

                    //System.out.println("Start Element :" + qName);

                    if (qName.equalsIgnoreCase("uploadTmpPath")) {
                        bUploadTmpPath = true;
                    }
                    if (qName.equalsIgnoreCase("storeFilePath")) {
                        bFileStorePath = true;
                    }

                    if (qName.equalsIgnoreCase("dbHost")) {
                        bDbHost = true;
                    }

                    if (qName.equalsIgnoreCase("dbUser")) {
                        bDbUser = true;
                    }
                    if (qName.equalsIgnoreCase("dbPwd")) {
                        bDbPwd = true;
                    }
                    if (qName.equalsIgnoreCase("dbPort")) {
                        bDbPort = true;
                    }
                    if (qName.equalsIgnoreCase("dbName")) {
                        bDbName = true;
                    }
                    if (qName.equalsIgnoreCase("wxUser")) {
                        bWxUser = true;
                    }
                    if (qName.equalsIgnoreCase("wxKey")) {
                        bWxKey = true;
                    }
                    if (qName.equalsIgnoreCase("wxToken")) {
                        bWxToken = true;
                    }
                    if (qName.equalsIgnoreCase("wxAesKey")) {
                        bWxAesKey = true;
                    }



                }

                public void endElement(String uri, String localName,
                                       String qName) throws SAXException {

                    //System.out.println("End Element :" + qName);

                }

                public void characters(char ch[], int start, int length) throws SAXException {

                    if (bUploadTmpPath) {
                        sUploadPath = new String(ch, start, length);
                        bUploadTmpPath = false;
                    }
                    if (bFileStorePath) {
                        sFileStorePath = new String(ch, start, length);
                        bFileStorePath = false;
                    }

                    if (bDbUser) {
                        sDbUser = new String(ch, start, length);
                        bDbUser = false;
                    }

                    if (bDbPwd) {
                        sDbPwd = new String(ch, start, length);
                        bDbPwd = false;
                    }
                    if (bDbPort) {
                        sDbPort = new String(ch, start, length);
                        bDbPort = false;
                    }
                    if (bDbHost) {
                        sDbHost = new String(ch, start, length);
                        bDbHost = false;
                    }
                    if (bDbName) {
                        sDbName = new String(ch, start, length);
                        bDbName = false;
                    }
                    if (bWxUser) {
                        sWxUser = new String(ch, start, length);
                        bWxUser = false;
                    }
                    if (bWxKey) {
                        sWxKey = new String(ch, start, length);
                        bWxKey = false;
                    }
                    if (bWxToken) {
                        sWxToken = new String(ch, start, length);
                        bWxToken = false;
                    }
                    if (bWxAesKey) {
                        sWxAesKey = new String(ch, start, length);
                        bWxAesKey = false;
                    }


                }

            };

            saxParser.parse("../config.xml", handler);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}